package com.wiserp.rest.helper;

import java.security.MessageDigest;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.wiserp.rest.model.OpResult;

public class LoginHelper extends JdbcDaoSupport {
	public Object login(String username, String password) {
		String enPass = encryptPassword(password);
		
		int count = this.getJdbcTemplate().queryForInt("select count(*) from saa01 where aaa001=? AND aaa004=? AND aaa012=1",username,enPass);
		if(count == 1)
			return  DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
		else
			return OpResult.AUTHFAIL;
	}

	public static String encryptPassword(String strPasswd) {
		if (strPasswd == null || strPasswd.equals("")) {
			return "";
		}
		MessageDigest md;
		byte[] ebytes = { 0 };
		try {
			md = MessageDigest.getInstance("MD5", "SUN");
			md.update(strPasswd.getBytes());
			ebytes = md.digest();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new sun.misc.BASE64Encoder().encode(ebytes);
	}

}
